import { IPlatform } from './IPlatform';

export class H5Platform implements IPlatform {
  async chooseVideo(): Promise<string> {
    return new Promise((resolve, reject) => {
      const input = document.createElement('input');
      input.type = 'file';
      input.accept = 'video/*';
      input.onchange = (e: any) => {
        const file = e.target.files[0];
        if (file) {
          resolve(URL.createObjectURL(file));
        } else {
          reject(new Error('未选择文件'));
        }
      };
      input.click();
    });
  }

  async saveSnapshot(canvas: HTMLCanvasElement): Promise<void> {
    const link = document.createElement('a');
    link.download = `截图_${Date.now()}.png`;
    link.href = canvas.toDataURL('image/png');
    link.click();
  }

  seekVideo(videoElement: any, time: number): void {
    // H5环境下需要获取实际的video元素
    const video = videoElement.$el ? videoElement.$el.querySelector('video') : videoElement;
    if (video) video.currentTime = time;
  }

  getVideoContext(videoElement: any): any {
    const video = videoElement.$el ? videoElement.$el.querySelector('video') : videoElement;
    return video;
  }

  initVideoListener(videoElement: any, callback: () => void): void {
    const video = videoElement.$el ? videoElement.$el.querySelector('video') : videoElement;
    if (video) {
      video.addEventListener('loadedmetadata', callback);
    }
  }
}